$(function () {
  const params = new URLSearchParams(location.search);
  $.ajax({
    url: 'http://localhost:3000/heroes/' + params.get('id'),
    type: "get"
  }).then(function (res) {
    const {err_code, message} = res;
    if (err_code !== 0) return layer.msg("数据读取错误," + message);
    $("#name").val(message[0].name);
    $("#age").val(message[0].age);

  });

  $('#editBtn').on('click', function () {
    const data = {
      name: $("#name").val(),
      age: $("#age").val()
    }
    layer.confirm('你确定要修改数据吗?', function () {
      $.ajax({
        url: "http://localhost:3000/heroes/" + params.get('id'),
        type: 'put',
        data: data
      }).then(function (res) {
        const {err_code, message} = res;
        if (err_code !== 0) return layer.msg("编辑失败," + message);
        layer.msg(message)
        setTimeout(function () {
          location.href = 'index.html';
        }, 1000);
      });
    })
  })
})